/*
* © 2010 Autodata Solutions Company. All Rights Reserved
* This source code is the confidential and proprietary information of Autodata Solutions Company.
* The user shall not, in whole or in part, copy, publish, disclose or make any use of this source code
* without the prior written consent of Autodata Solutions Company.
*/
Ext.ns('net.autodata.compare.plugins');
(function() {
    var EVENTS = net.autodata.EventBus.EVENTS;

    /**
    * This plugin is used to bind the ResultsGrid to the events of the EventBus.
    * This will keep the ResultsGrid always insync with the display of the CompareVehiclesPanel.
    * @class ResultsGridEventBusConnector
    * @extends Ext.util.Observable
    * @namespace net.autodata.compare.plugins
    */
    var Plugin = Ext.extend(Ext.util.Observable, {
        /**
        * @property inventoryPanel
        * @type net.autodata.compare.InventoryPanel
        */
        resultGrid: undefined,

        /**
        * @method init
        * @param {net.autodata.compare.ResultGrid} resultGrid
        */
        init: function(resultGrid) {
            this.resultGrid = resultGrid;
            var eb = net.autodata.EventBus.getInstance();
            eb.on(EVENTS.READY, this.loadGrid, this);
            eb.on(EVENTS.SESSION_COMPETITOR_CHANGED, this.loadGrid, this);
            eb.on(EVENTS.SESSION_COMPETITOR_REMOVED, this.loadGrid, this);
        },

        loadGrid: function() {
            var session = net.autodata.Session.getInstance();
            var baseVehicle = session.vehicles.getBaseVehicle().acode;
            var competitors = [];
            if (session.vehicles.hasCompetitors()) {
                session.vehicles.eachCompetitor(function(competitor) {
                        competitors.push(competitor && competitor.acode ? competitor.acode : '');
                }, this);
            }

            var store = this.resultGrid.getStore();
            store.removeAll();

            if (!competitors || competitors.length <= 0) {
                this.resultGrid.hide();
                return;
            }

            this.resultGrid.show();

            Ext.apply(store.baseParams, {
                baseAcode: baseVehicle,
                competitorAcodes: []
            });

            Ext.each(competitors, function(competitor) {
                store.baseParams.competitorAcodes.push(competitor ? competitor : '');
            });

            store.load();
        },


        /**
        * Used to clean up all event bindings made by this plugin
        * @private
        */
        onDestroy: function() {
            var eb = net.autodata.EventBus.getInstance();
            eb.un(EVENTS.READY, this.loadGrid, this);
            eb.un(EVENTS.SESSION_COMPETITOR_CHANGED, this.loadGrid, this);
            eb.un(EVENTS.SESSION_COMPETITOR_REMOVED, this.loadGrid, this);
        }

    });

    net.autodata.compare.plugins.ResultGridEventBusConnector = Plugin;

})();